//
//  CCAListTableViewCell.m
//  Activity
//
//  Created by its on 15/5/14.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCAListTableViewCell.h"
#import "CCDevice.h"
#import "CCAListCellViewModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Masonry/Masonry.h>
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface CCAListTableViewCell()
@property (nonatomic, strong) CCAListCellViewModel *viewModel;
@end

@implementation CCAListTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
- (void) bindViewModel:(id)viewModel {
  _viewModel = viewModel;
  [self bindUI];
}

- (void) bindUI {
  [self.bgImageView sd_setImageWithURL:_viewModel.bgURL];
  [self.headImageView sd_setImageWithURL:_viewModel.headURL];
  self.nameLabel.attributedText = _viewModel.name;
  self.heartImageView.image = _viewModel.heardImage;
  self.heartLabel.attributedText = _viewModel.heard;
  self.addressIcon.image = _viewModel.addressImage;
  self.addressLabel.attributedText = _viewModel.address;
  self.timeIcon.image = _viewModel.timeImage;
  self.timeLabel.attributedText = _viewModel.time;
  self.peopleIcon.image = _viewModel.peopleImage;
  self.peopleLabel.attributedText = _viewModel.people;
  self.moneyIcon.image = _viewModel.moneyImage;
  self.moneyLabel.attributedText = _viewModel.money;
  self.titleLabel.attributedText = _viewModel.title;
}

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    [self setupUI];
    [self viewWithCSS];
  }
  return self;
}

- (void) setupUI {
  
       self.bgImageView = [UIImageView new];
       [self.contentView addSubview:self.bgImageView];

  self.titleLabel = [UILabel new];
       [self.titleLabel sizeToFit];
       [self.contentView addSubview: self.titleLabel];
  
  self.peopleIcon = [UIImageView new];
       self.peopleLabel = [UILabel new];
       [self.contentView addSubview:self.peopleIcon];
       [self.contentView addSubview: self.peopleLabel];
  
  self.moneyIcon = [UIImageView new];
       self.moneyLabel = [UILabel new];
       [self.contentView addSubview: self.moneyIcon];
       [self.contentView addSubview: self.moneyLabel];
  
  self.addressIcon = [UIImageView new];
       self.addressLabel = [UILabel new];
       [self.contentView addSubview:self.addressIcon];
       [self.contentView addSubview: self.addressLabel];

  self.timeIcon = [UIImageView new];
       self.timeLabel = [UILabel new];
       [self.contentView addSubview: self.timeIcon];
       [self.contentView addSubview: self.timeLabel];

  self.headImageView = [UIImageView new];
  self.headImageView.layer.cornerRadius = 35 / 2;
  self.headImageView.clipsToBounds = YES;
  self.headImageView.layer.borderWidth = 1.0f;
  self.headImageView.layer.borderColor = [UIColor whiteColor].CGColor;
       self.nameLabel = [UILabel new];
       [self.contentView addSubview: self.headImageView];
       [self.contentView addSubview: self.nameLabel];

  self.heartImageView = [UIImageView new];
       self.heartLabel = [UILabel new];
       [self.contentView addSubview: self.heartImageView];
       [self.contentView addSubview: self.heartLabel];
  
}

- (void)viewWithCSS {
  @weakify(self);
  [self.bgImageView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(0);
    make.top.mas_equalTo(0);
    make.size.mas_equalTo(CGSizeMake(DEVICEWIDTH, DEVICEWIDTH * 0.8));
  }];
  
  [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.size.equalTo(self.bgImageView);
  }];
  
  [self.headImageView mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.top.equalTo(self.contentView).offset(5);
    make.leading.equalTo(self.contentView).offset(5);
    make.size.mas_equalTo(CGSizeMake(35, 35));
  }];
  [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.headImageView.mas_right).offset(2);
    make.centerY.equalTo(self.headImageView);
    make.right.equalTo(self.heartImageView.mas_left).offset(-2);
  }];
  
  [self.heartImageView mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.right.equalTo(self.contentView).offset(-5);
    make.top.equalTo(self.contentView).offset(-5);
    make.size.mas_equalTo(CGSizeMake(65, 65));
  }];
  [self.heartLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.bottom.equalTo(self.heartImageView);
    make.centerY.equalTo(self.heartImageView);
    make.centerX.equalTo(self.heartImageView);
  }];
  
  [self.addressIcon mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.contentView).offset(5);
    make.bottom.equalTo(self.contentView).offset(-5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  [self.addressLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.addressIcon.mas_right).offset(2);
    make.top.equalTo(self.addressIcon);
    make.bottom.equalTo(self.addressIcon);
    make.right.equalTo(self.timeIcon.mas_left).offset(2);
  }];
  
  [self.timeIcon mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.contentView).offset(DEVICEWIDTH / 2);
    make.bottom.equalTo(self.contentView).offset(-5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.timeIcon.mas_right).offset(2);
    make.top.equalTo(self.timeIcon);
    make.bottom.equalTo(self.timeIcon);
    make.right.equalTo(self.contentView).offset(-2);
  }];
  
  [self.peopleIcon mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.contentView).offset(5);
    make.bottom.equalTo(self.addressIcon.mas_top).offset(-5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  [self.peopleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.peopleIcon.mas_right).offset(2);
    make.top.equalTo(self.peopleIcon);
    make.bottom.equalTo(self.peopleIcon);
    make.right.equalTo(self.moneyIcon).offset(-2);
  }];
  
  [self.moneyIcon mas_updateConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.contentView).offset(DEVICEWIDTH / 2);
    make.bottom.equalTo(self.timeIcon.mas_top).offset(-5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.moneyIcon.mas_right).offset(2);
    make.top.equalTo(self.moneyIcon);
    make.bottom.equalTo(self.moneyIcon);
    make.right.equalTo(self.contentView).offset(-2);
  }];
  
  [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    @strongify(self);
    make.left.equalTo(self.contentView).offset(5);
    make.bottom.equalTo(self.moneyIcon.mas_top).offset(-5);
  }];
}
@end
